﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Threading;
using System.Globalization;

namespace ISolutions.SharePoint.Mui.MuiLanguageSwitchWebPart
{
    /// <summary>
    /// MUI Language Filter Web Part
    /// 
    /// by David Schneider, isolutions AG, http://blog.sharepoint.ch
    /// </summary>
    [ToolboxItemAttribute(false)]
    public class MuiLanguageSwitchWebPart : WebPart
    {
        /// <summary>
        /// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object and stores tracing information about the control if tracing is enabled.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HTmlTextWriter"/> object that receives the control content.</param>
        public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
        {
            base.RenderControl(writer);

            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            foreach (CultureInfo culture in SPContext.Current.Web.SupportedUICultures)
            {
                if (culture.LCID == Thread.CurrentThread.CurrentUICulture.LCID)
                {
                    writer.AddAttribute("style", "font-weight:bold");
                }

                writer.AddAttribute("onclick", "window.location='javascript:OnSelectionChange(" + culture.LCID + ");';");
                writer.AddAttribute("href", "#");
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write(culture.TwoLetterISOLanguageName.ToUpper() + "&nbsp;");
                writer.RenderEndTag();
            }
            writer.RenderEndTag();
        }

    }
}
